* Replication Code: Did the Budapest Convention Deter Cybercrime? An Event Study Using a Panel of Countries
* Authors: Humza Khan and Pengfei Zhang
* Data: Cyber_Espionage

* Install programs
ssc install reghdfe
ssc install event_plot
ssc install did_imputation
ssc install csdid
ssc install drdid
ssc install ivreg2
ssc install avar
ssc install eventstudyinteract
ssc install did_multiplegt

* Import data 

** converts strings (characters/text) to numeric
destring, replace

*unique numeric identifier for each country
gen country_num = _n 

*Reshape the data from wide to long format
reshape long attack GDP Internet Education, i(country_num) j(year)

tsset country_num year //dataset as a time series dataset  

*Create the treatment variable, i.e., timeToTreat 
gen timeToTreat = year - Eventtime 
gen treatment = cond(year >= Eventtime,1,0)

* preserve the dataset for data analysis manipulation
preserve

*1 OLS  
regress attack treatment

*2 only "country" fixed effect
xtreg attack treatment, fe

*3 twfe
reghdfe attack treatment, absorb(country_num year) cluster(country_num)

*4 twfe plus controls
reghdfe attack treatment GDP Internet Education, absorb(country_num year)

*drop observations based on time window
drop if timeToTreat < -17 
drop if timeToTreat > 17

*leads and lags
forvalues l = 0/17 {  
		gen L`l'event = timeToTreat == `l'
	} 
forvalues l = 1/17 { 
		gen F`l'event = timeToTreat ==-`l' 
} 
drop F1event  

* check event window
tab timeToTreat

*Dynamic Regression  
reghdfe attack F*event L*event, absorb(country_num year) cluster(country_num)
estimates store ols  

*event plot ols
	event_plot ols, ///
	stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
	graph_opt(ytitle("Estimated Change in Cyber Espionage") ylabel(, labsize(small)) xlabel(-17(1)16, labsize(small)) xtitle("Years Before and After Ratification") ///
		legend(off) /// 
		xline(-1, lcolor(gs13) lpattern(dash)) yline(0, lcolor(gs13)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
	lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black))
 
******************************************
*** Graph: DiD Estimator Comparisons ***
******************************************

*** Borusyak et al. ***
*ssc install did_imputation
did_imputation attack country_num year Eventtime, autosample minn(0) allhorizons pretrends(17) cluster(country_num) 
estimates store bjs  

*** Callaway and San'Anna *** SHOWS ALL ZERO VALUES
gen gvar = cond(Eventtime==., 0, Eventtime)  
csdid attack, ivar(country_num) time(year) gvar(gvar) notyet event
estat event, estore(cs)  

*** Sun and Abraham ***
gen lastcohort = Eventtime==r(max)
eventstudyinteract attack L*event F*event, vce(cluster country_num) absorb(country_num year) cohort(Eventtime) control_cohort(lastcohort)
matrix sa_b = e(b_iw)  
matrix sa_v = e(V_iw)

*** de Chaisemartin-D'Haultfoeuille ***
did_multiplegt_old attack country_num year treatment, robust_dynamic dynamic(12) placebo(4) breps(100) cluster(country_num)
matrix dcdh_b = e(estimates)
matrix dcdh_v = e(variances)

*HTE estimator graph
event_plot bjs dcdh_b#dcdh_v cs sa_b#sa_v ols, ///
	stub_lag(tau# Effect_# Tp# L#event L#event) stub_lead(pre# Placebo_# Tm# F#event F#event) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.325(0.13)0.325) trimlead(10) trimlag(10) noautolegend ///
	graph_opt( ///
		xtitle("Years Before and After Ratification",size(small)) ytitle("Estimated Change in Cyber Espionage",size(small)) xlabel(-10(1)10, labsize(vsmall)) ylabel(-2(1)6, labsize(vsmall)) ///
		legend(order(1 "Borusyak et al." 3 "de Chaisemartin-D'Haultfoeuille" ///
				5 "Callaway-Sant'Anna" 7 "Sun-Abraham" 9 "OLS") rows(2) position(6) region(lcolor(black) lpattern(longdash)) size(small) placement(center) lalign(center)) ///
		xline(-1, lcolor(gs12) lpattern(dash)) yline(0, lpattern(dash) lcolor(gs12)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal) labsize(vsmall)) ///
	) ///
	lag_opt1(msymbol(+) color(red)) lag_ci_opt1(color(red)) ///
	lag_opt2(msymbol(O) color(orange)) lag_ci_opt2(color(orange)) ///
	lag_opt3(msymbol(Dh) color(blue)) lag_ci_opt3(color(blue)) ///
	lag_opt4(msymbol(Th) color(emerald)) lag_ci_opt4(color(emerald)) ///
	lag_opt5(msymbol(Sh) color(black)) lag_ci_opt5(color(black)) 











